<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Crypt;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    //add
    public function add()
    {

    	return view('admin.user.add');
    }
    
    //insert
    public function insert(Request $request)
    {
    	

    	$this ->validate($request,[
    			'username' => 'required',
    			'password' => 'required',
    			'repassword' => 'required|same:password',
    			'email' => 'required|email',
    			'phone' => 'required|max:11|min:11',
    		],[
    			'username.required' => '用户名不能为空',
    			'password.required' => '密码不能为空',
    			'repassword.required' =>'确认密码不能为空',
    			'repassword.same' =>'两次密码不一样',
    			'email.required' =>'邮箱不能为空',
    			'email.email' => '邮箱格式不正确',
    			'phone.required' =>'手机号不能为空',
    			'phone.max' =>'手机号必须是11位的',
    			'phone.min' =>'手机号必须是11位的',
    		]);
    	//处理好数据
    	$data = $request -> except('_token','repassword');
    	// dd($data);

    	//加密密码
    	$data['password'] = Crypt::encrypt($data['password']);
    	// dd($data);

    	$data['remember_token'] = str_random(70);
    	$time = date("Y-m-d H:i:s");
    	$data['created_at'] = $time;
    	$data['updated_at'] = $time;

    	//添加数据
    	$res = DB::table('users') -> insert($data);
    	if($res)
    	{
    		return redirect('/admin/user/index') -> with(['info' => '添加成功']);

    	}else
    	{
    		return redirect('/admin/user/index') -> with(['info' => '添加失败']);
    	}
	 }

	 //index
	 public function index(Request $request)
	 {
	 	//查询
	 	$num = $request -> input('num',10);
	 	$data = DB::table('users')
	 		->where(function($query) use ($request){
	 			$query -> where('username', 'like', '%'.$request -> input('keyword').'%');
	 		})
	 		->paginate($num);
	 	

	 	return view('admin.user.index', ['data' => $data, 'request'=>$request -> all()]);
	 }

	 // 编辑
	 public function edit($id)
	 {
	 	// return $id;
	 	$data = DB::table('users') -> where('id', $id) -> first();
	 	// dd($data);
	 	return view('admin.user.edit', ['data'=> $data]);
	 }

	 //更新
	 public function update(Request $request)
	 {
	 	// dd($request -> all());
	 	$this -> validate($request,[
	 			'username' => 'required',
	 			'email' => 'required|email',
	 			'phone' => 'required|max:11|min:11'
	 		],[
	 			'username.required' => '用户名不能为空',
	 			'email.required' => '邮箱不能为空',
	 			'email.email' => '邮箱格式不正确',
	 			'phone.required' => '手机号不能为空',
	 			'phone.max' => '手机号必须是11位的',
	 			'phone.min' => '手机号必须是11位的',
	 		]);
	 	$data = $request ->except('_token', 'id');
	 	$data['updated_at'] = date('Y-m-d H:i:s');

	 	// 执行更新
	 	$res = DB::table('users') -> where('id', $request -> input('id'))-> update($data);
	 	//判断
	 	if($res)
	 	{
	 		return redirect('/admin/user/index') -> with(['info' => '更新成功']);
	 	}else
	 	{
	 		return back() -> with(['info' => '更新失败']);
	 	}
	 }


	 //删除
	 public function delete($id)
	 {
	 	// return $id;
	 	$res = DB::table('users') -> where('id', $id) -> delete();
	 	//判断
	 	if($res)
	 	{
	 		return redirect('/admin/user/index') -> with(['info' => '删除成功']);
	 	}else
	 	{
	 		return back() -> with(['info' => '删除失败']);
	 	}
	 }

	 //ajaxUpdate
	 public function ajaxUpdate(Request $request)
	 {
	 	// return response() -> json(0);
	 	$id = $request -> input('id');
	 	//根据ID查数据
	 	$data = DB::table('users') -> where('id', $id) ->first();
	 	//判断
	 	if($data -> status =='0')
	 	{
	 		$res = DB::table('users') -> where('id', $id) ->update(['status'=> '1']);
	 		if($res)
	 		{
	 			return response() -> json(0);
	 		}else
	 		{
	 			return response() -> json(1);
	 		}
	 	}else
	 	{
	 		$res = DB::table('users') -> where('id', $id) ->update(['status'=> '0']);
	 		if($res)
	 		{
	 			return response() -> json(2);
	 		}else
	 		{
	 			return response() -> json(3);
	 		}
	 	}

	 }

	 //ajaxUsername
	 public function ajaxUsername(Request $request)
	 {
	 	$name = $request -> input('name');
	 	//注:这个name是post传过来的;
	 	//查询
	 	$res = DB::table('users') -> where('username', $name)
	 							  -> first();

	 	if($res)
	 	{
	 		//1代表用户名已经存在
	 		return response() -> json(1);
	 	}else
	 	{
	 		$res = DB::table('users') -> where('id', $request -> input('id'))
	 								  -> update(['username' => $name]);

	 		if($res)
	 		{
	 			return response() -> json(0);
	 		}else
	 		{
	 			return response() -> json(2);
	 		}
	 	}


	 }

}


